﻿@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>同步接口</title>
    <script src="~/lib/jquery/dist/jquery.js"></script>
</head>
<body>
    <span>接口数据</span>
    <textarea style="width: 400px; height:300px" id="apiJson" name="apiJson"></textarea>
    <input type="button" id="btnSubmit" name="name" value="同步" />
    &nbsp; &nbsp; &nbsp;
    <span>解析后</span>
    <textarea style="width: 400px; height:300px" id="apiJson2" name="apiJson2"></textarea>
    <br />
    <br />
    <input type="text" id="txtController1" name="txtController1" value="" />
    <input type="button" id="btnAdd" name="name" value="添加控制器" />
    <br />
    <br />
    <input type="text" id="txtController2" name="txtController2" value="" />
    <input type="button" id="btnUpdate" name="name" value="更新控制器" />
    <script>

        $(function () {

            var apiObject = {};
            apiObject.total = 0;
            apiObject.list = [];
            apiObject.dtos = [];

            $.ajax({
                type: "GET",
                url: "/swagger/v1.0/swagger.json",
                data: {},
                dataType: "json",
                success: function (data) {
                    //处理接口url和注释
                    var apiObj = data.paths;
                    for (var api in apiObj) {
                        var url = api;
                        var summary = ""
                        //console.log(api + ":" + apiObj[api]);
                        for (var m in apiObj[api]) {
                            //console.log(m + ":" + apiObj[api][m].summary);
                            //console.log(api); //接口地址

                            var o = {
                                url: api,
                                method: m,
                                parameters: [],
                                summary: apiObj[api][m].summary,
                                reqDtoKey: '',
                                respDtoKey: ''
                            };

                            //处理参数
                            if (apiObj[api][m].parameters) {
                                o.parameters = apiObj[api][m].parameters
                            }

                            //处理返回dto名字
                            if (!apiObj[api][m].responses['200'] || !apiObj[api][m].responses['200'].content || !("application/json" in apiObj[api][m].responses['200'].content && apiObj[api][m].responses['200'].content["application/json"].schema)) {
                                continue
                            }
                            let respKey = apiObj[api][m].responses['200'].content["application/json"].schema.$ref.replace('#/components/schemas/', '');
                            o.respDtoKey = respKey

                            //处理请求dto名字
                            if (!apiObj[api][m].requestBody || !apiObj[api][m].requestBody.content['application/json'] || !apiObj[api][m].requestBody.content['application/json'].schema.$ref) {
                                continue
                            }
                            let reqKey = apiObj[api][m].requestBody.content['application/json'].schema.$ref.replace('#/components/schemas/', '');
                            o.reqDtoKey = reqKey

                        }

                        apiObject.total++;
                        apiObject.list.push(o);
                    }

                    //处理dto字段
                    for (var key in data.components.schemas) {
                        let dto = {}
                        dto.name = key
                        dto.properties = []
                        dto.required = []
                        for (var p in data.components.schemas[key].properties) {
                            var field = {}
                            var prop = data.components.schemas[key].properties[p]
                            field.name = p;
                            field.type = prop.type ? prop.type : '';
                            field.desc = prop.description ? prop.description : '';
                            field.nullable = prop.nullable ? true : false;
                            field.format = prop.format ? prop.format : '';
                            field.maxLength = prop.maxLength ? prop.maxLength : 0;
                            field.minLength = prop.minLength ? prop.minLength : 0;
                            dto.properties.push(field);
                        }
                        //必填字段 加notnull的
                        if (data.components.schemas[key].required) {
                            dto.required = data.components.schemas[key].required;
                        }
                        apiObject.dtos.push(dto)
                    }

                    console.log(apiObject);
                    //console.log(JSON.stringify(apiObject));

                    $('#apiJson').val(JSON.stringify(data));    //JSON.parse(jsonstr);
                    $('#apiJson2').val(JSON.stringify(apiObject));

                }
            });


            $('#btnSubmit').click(function () {
                if (confirm("此操作将会添加、更新绑定、删除多余")) {
                    $.ajax({
                        type: "post",
                        url: "/BasePlatform/SysApi/SyncApi",
                        contentType: "application/json",
                        data: JSON.stringify(apiObject),
                        async: true,
                        dataType: "json",
                        success: function (data) {
                            console.log(data);
                            alert("添加了" + data.body.addCount + "个，更新了" + data.body.updCount + "个，删除了" + data.body.delCount + "个");
                        }
                    });
                }
            });

            $('#btnAdd').click(function () {
                var cName = $('#txtController1').val();
                if (!cName) {
                    alert("名字不能为空");
                    return;
                }
                $.ajax({
                    type: "post",
                    url: "/BasePlatform/SysApi/AddApi8Controller?controllName=" + cName,
                    contentType: "application/json",
                    data: JSON.stringify(apiObject),
                    async: true,
                    dataType: "json",
                    success: function (data) {
                        console.log(data);
                        alert("添加了" + data.body.addCount + "个，更新了" + data.body.updCount + "个，删除了" + data.body.delCount + "个");
                    }
                });

            });

            $('#btnUpdate').click(function () {
                var cName = $('#txtController2').val();
                if (!cName) {
                    alert("名字不能为空");
                    return;
                }
                $.ajax({
                    type: "post",
                    url: "/@(ViewBag.PlatformApiPath)/SysApi/UpdateApi8Controller?controllName=" + cName,
                    contentType: "application/json",
                    data: JSON.stringify(apiObject),
                    async: true,
                    dataType: "json",
                    success: function (data) {
                        console.log(data);
                        alert("添加了" + data.body.addCount + "个，更新了" + data.body.updCount + "个，删除了" + data.body.delCount + "个");
                    }
                });

            });

        });









        /**
        * 取出中括号内的内容
        */
        function getBracketStr(text) {
            let result = ''
            if (isObjEmpty(text))
                return result
            let regex = /\[(.+?)\]/g;
            let options = text.match(regex)
            if (!isObjEmpty(options)) {
                let option = options[0]
                if (!isObjEmpty(option)) {
                    result = option.substring(1, option.length - 1)
                }
            }
            return result
        }
        //getBracketStr('JsonR[JsonPage[Json_SysApi]]')

        /**
         * 取出小括号内的内容
         */
        function getParenthesesStr(text) {
            let result = ''
            if (isObjEmpty(text))
                return result
            let regex = /\((.+?)\)/g;
            let options = text.match(regex)
            if (!isObjEmpty(options)) {
                let option = options[0]
                if (!isObjEmpty(option)) {
                    result = option.substring(1, option.length - 1)
                }
            }
            return result
        }

        function isObjEmpty(text) {
            if (!text) {
                return true;
            }
        }

    </script>

</body>
</html>
